using System.Collections.Generic;
using Build.Interface;
using Config;
using Data.MapObject;
using GameTime;
using Map.MapObject.HitObject;
using MoonFramework.Scripts.Event;
using MoonFramework.Template;
using Tool;
using Tool.Name;
using UnityEngine;

namespace Map.MapObject.PickUpObject
{
    public class BerryBushMapObject : BasePickUpMapObject, IBuilding
    {
        [SerializeField] private AudioClip[] hurtAudioClips;
        [SerializeField] private MeshRenderer _meshRenderer;
        [SerializeField] private Material[] _materials; // 0是有果子的，1是没果子的
        [SerializeField] private int berryGrowDays = 2;     //浆果成熟天数
        private BerryBushTypeData _typeData;

        public override void Init(BaseMapBlock mapBlock, ulong id)
        {
            base.Init(mapBlock, id); 
            if (ArchiveManager.Instance.TryGetIMapObjectTypeData(id, out var tempData))
            {
                _typeData = tempData as BerryBushTypeData;
            }
            else
            {
                _typeData = new();
                ArchiveManager.Instance.AddIMapObjectTypeData(id, _typeData);
            }
            //TODO:如果来自建筑物，直接视为刚刚采摘（这件事情也需要持久化）
            CheckAndSetState();
            EventCenter.Instance.SendCommand(EventName.OnMorning, OnMorning);
        }
        
        protected override void Dead()
        {
            //修改外表
            //TODO:持久化
            _meshRenderer.sharedMaterial = _materials[1];
            _typeData.lastPickUpDayNum = TimeManager.Instance.CurDay;
            EventCenter.Instance.Callback(EventName.AddItem, _pickupConfigID);
            EventCenter.Instance.Callback(EventName.RemoveMapObject, gameObject.GetComponent<Collider>(), false);
            EventCenter.Instance.RemoveCommand<ItemWeaponInfo>(EventName.Hurt, Hurt);
            var bushLeafObject = gameObject.AddComponent<BushLeafObject>();
            bushLeafObject.Init(_mapBlock, _id);
            bushLeafObject.hp = bushLeafObject.MaxHp = 3f;
            bushLeafObject.hurtAudioClips = hurtAudioClips;
            enabled = false;
        }
        
        private void CheckAndSetState()
        {
            if (_typeData.lastPickUpDayNum == -1)
            {
                _meshRenderer.sharedMaterial = _materials[0];
            }
            else
            {
                //根据天数确定状态
                _meshRenderer.sharedMaterial = _materials[1];
                if (TimeManager.Instance.CurDay - _typeData.lastPickUpDayNum >= berryGrowDays)
                {
                    
                }
            }
        }

        private void OnMorning()
        {
            //如果已经成熟，无需检测
            CheckAndSetState();
        }
        
        #region 建筑物
        [SerializeField] private Collider _collider;
        private List<Material> _materialList;
        public GameObject BuildingObj => gameObject;
        public Collider Collider => _collider;
        public List<Material> Materials { get => _materialList; set => _materialList = value; }

        public void OnPreview()
        {
        }

        public bool CollisionDetectionOnPreview(LayerMask layerMask)
        {
            //TODO:待重构
            return true;
        }
        #endregion
    }
}